<?php
/**
 * 备货申请明细（备货单明细）
 * (model)
 */
namespace App\Models\Stocking;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
class StockingApplyDetail extends Model
{
    protected $table = 's_stocking_apply_detail';

    protected $guarded = [];

    /**
     * 单据状态
     * @var array
     */
    const STATUS_AUDIT = 1; //已审核
    const STATUS_CLOSE = 2; //已关闭
    public static $_status = [
        self::STATUS_AUDIT => '已审核',
        self::STATUS_CLOSE => '已关闭',
    ];

    /**
     * 计划状态
     * @var array
     */
    const PLAN_STATUS_NO = 1; //未计划
    const PLAN_STATUS_PART = 2; //部分计划
    const PLAN_STATUS_ALL = 3; //完全计划
    public static $_planStatus = [
        self::PLAN_STATUS_NO => '未计划',
        self::PLAN_STATUS_PART => '部分计划',
        self::PLAN_STATUS_ALL => '完全计划',
    ];

    /**
     * 采购状态
     * @var array
     */
    const PURCHASE_STATUS_NO = 1; //未下单
    const PURCHASE_STATUS_YES = 2; //已下单
    const PURCHASE_STATUS_CLOSE = 3; //已关闭
    public static $_purStatus = [
        self::PURCHASE_STATUS_NO => '未下单',
        self::PURCHASE_STATUS_YES => '已下单',
        self::PURCHASE_STATUS_CLOSE => '已关闭'
    ];

    /**
     * 收获状态
     * @var array
     */
    const RECEIVED_NO = 1; //未到货
    const RECEIVED_PART = 2; //部分到货
    const RECEIVED_ALL = 3; //完全到货
    public static $_reStatus = [
        self::RECEIVED_NO => '未到货',
        self::RECEIVED_PART => '部分到货',
        self::RECEIVED_ALL => '完全到货',
    ];

    public function apply(){
        return $this->belongsTo(StockingApply::class,'stocking_no','stocking_no');
    }

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    /**
     * MAX(1)未
     * MIN(3)已
     * ELSE 部分
     * @param $status
     * @return int
     */
    public static function extractStatus($status){
        if(max($status) == 1)return 1;
        if(min($status) == 3)return 3;
        return 2;
    }
}
